package com.ljh.mall.mallsearch.thread;

import java.util.concurrent.*;

/**
 * @author ：刘剑鸿
 * @date ：Created in 2023/3/19 20:37
 * @description：
 * @modified By：
 * @version: $
 */
public class ThreadDemo {
    // 定一个线程池对象
    private static ExecutorService service = Executors.newFixedThreadPool(5);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadDemo01 threadDemo01 = new ThreadDemo01();
        threadDemo01.start();

         Thread thread = new Thread(()->{
            System.out.println("当前线程"+ Thread.currentThread().getName());
        });
        thread.start();
        ThreadDemo2 threadDemo2 = new ThreadDemo2();
        new Thread(threadDemo2).start();

        // 通过Callable 接口来实现。 FutureTask本质上是一个Runnable接口
        FutureTask futureTask = new FutureTask(new Mycallable());
        Thread thread1 = new Thread(futureTask);
        thread1.start();

        // 阻塞等待子线程的执行完成，然后获取线程的返回结果
        Object o = futureTask.get();
        System.out.println("o = " + o);






        service.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程池--》当前线程:" + Thread.currentThread().getName());
            }
        });



    }

}

class Mycallable implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println("当前线程Mycallable:" + Thread.currentThread().getName());
        return 10;
    }
}
class ThreadDemo01 extends Thread {
    @Override
    public void run() {
        System.out.println("当前线程"+ Thread.currentThread().getName());
    }
}
class ThreadDemo2 implements Runnable{

    @Override
    public void run() {
        System.out.println("当前线程:" + Thread.currentThread().getName());
    }
}
